//
//  AnotherFireViewController.m
//  AnotherFire
//
//  Created by pcwiz on 23/06/09.
//  Copyright leowiz 2009. All rights reserved.
//

#import "AnotherFireViewController.h"

@implementation AnotherFireViewController
@synthesize gv;

@synthesize lblFirstName;
@synthesize lblSecondName;
@synthesize tbFirstName;
@synthesize tbSecondName;
@synthesize btnPlay;

- (void)dealloc {
	[self,gv dealloc];
	[self.view dealloc];
    [super dealloc];
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	self.view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
	
	self.lblFirstName = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 150, 30)];
	self.lblFirstName.text = @"First Player:";
	[self.view addSubview: self.lblFirstName];
	
	self.lblSecondName = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 150, 30)];
	self.lblSecondName.text = @"Second Player:";
	[self.view addSubview: self.lblSecondName];
	
	self.tbFirstName = [[UITextField alloc] initWithFrame:CGRectMake(200, 150, 120, 30)];
	self.tbFirstName.text = @"First";
	[self.view addSubview: self.tbFirstName];
	
	self.tbSecondName = [[UITextField alloc] initWithFrame:CGRectMake(200, 200, 120, 30)];
	self.tbSecondName.text = @"Second";
	[self.view addSubview: self.tbSecondName];
	
	/*
	self.btnPlay = [[UIButton alloc] initWithFrame:CGRectMake(100, 250, 150, 30)];
	[self.btnPlay setState:UIControlStateNormal];
	[self.btnPlay setTitle:@"Mooo" forState:UIControlStateNormal];
	//[self.btnPlay setTitle:@"Moo"];*/
	
	self.btnPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[self.btnPlay setTitle:@"Mooo" forState:UIControlStateNormal];
	[self.btnPlay setFrame:CGRectMake(70, 250, 150, 30)];
	[self.btnPlay addTarget:self action:@selector(settingsChanged) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview: self.btnPlay];
	
}

- (void) touchOccured: (CGPoint) _where;
{
	[super touchOccured:_where];
	[self.gv setNeedsDisplay];
}

-(void) settingsChanged
{
	[super initWithNames:self.tbFirstName.text second:self.tbSecondName.text];

	[self.btnPlay removeFromSuperview];
	[self.lblFirstName removeFromSuperview];
	[self.lblSecondName removeFromSuperview];
	[self.tbFirstName removeFromSuperview];
	[self.tbSecondName removeFromSuperview];
	
	[self startGame];
}

//Actually start the game with that number of players:
-(void) startGame
{
	printf("Starting the game.\n");
	self.gv = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	[self.gv setNeedsDisplay];
	[self.gv newParent: self];
	[self.view addSubview: self.gv];

}

-(void) alertView: (UIAlertView *)_alert_view clickedButtonAtIndex:(NSInteger)_button
{
	printf("A new game is beggining.");
	
	[self.gl startGame];
	[self.gv removeFromSuperview];
	[self.gv dealloc];
	[self startGame];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

@end
